package cn.ynrcb.personnelfile.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.ynrcb.personnelfile.admin.dao.SmsLogDao;
import cn.ynrcb.personnelfile.admin.entity.SmsLogEntity;
import cn.ynrcb.personnelfile.admin.enums.SmsStatusEnum;
import cn.ynrcb.personnelfile.admin.enums.SmsTypeEnum;
import cn.ynrcb.personnelfile.admin.service.SmsLogService;
import cn.ynrcb.personnelfile.admin.vo.SmsLogVo;
import cn.ynrcb.personnelfile.common.utils.EnumUtil;
import cn.ynrcb.personnelfile.common.utils.PageUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("smsLogService")
public class SmsLogServiceImpl extends ServiceImpl<SmsLogDao, SmsLogEntity> implements SmsLogService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Page<SmsLogVo> page = new Page<SmsLogVo>(Long.valueOf(params.get("page") + ""), Long.valueOf(params.get("limit") + ""));
        List<SmsLogVo> list = baseMapper.listOfOpLog(page, params);
        //将列表中的枚举字段进行替换
        page.setRecords(replaceList(list));
        PageUtils pageData = new PageUtils((IPage<?>) page);
        return pageData;


    }

    @Override
    public SmsLogEntity findSmsLog(String phone, Integer type, Integer status) {
        List<SmsLogEntity> list = baseMapper.getNewList(phone, type, status);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 将返回数据中的枚举数据替换成枚举信息，可直接用于显示
     *
     * @param list
     * @return
     */
    public List<SmsLogVo> replaceList(List<SmsLogVo> list) {
        if (CollectionUtil.isNotEmpty(list)) {
            for (SmsLogVo vo : list) {
                vo.setTypeName(EnumUtil.getByCode(SmsTypeEnum.class,vo.getType()).getMessage());
                vo.setStatusName(EnumUtil.getByCode(SmsStatusEnum.class,vo.getStatus()).getMessage());
            }
        }
        return list;
    }

}
